package com.training.android.tictactoe;

import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

public class GameActivity extends Activity {

	GridView gridView;
	CustomListBaseAdapter adapter;
	int player = 0;
	int player1Marks = 0;
	int player2Marks = 0;
	int gameMode = 0;
	boolean gameOver = false;
	Intent currentIntent;
	ArrayList<Integer> playedGrid = new ArrayList<Integer>();
	ArrayList<EachGrid> CartoonList = new ArrayList<EachGrid>();
	int[][] winning = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 0, 3, 6 },
			{ 1, 4, 7 }, { 2, 5, 8 }, { 0, 4, 8 }, { 2, 4, 6 } };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_game);

		currentIntent = getIntent();
		gameMode = currentIntent.getExtras().getInt("gameMode");

		getData();
		gridView = (GridView) findViewById(R.id.gameGridView);
		adapter = new CustomListBaseAdapter(this, CartoonList);
		gridView.setAdapter(adapter);

		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				gameGridClicked(position);
			}
		});
	}

	private void getData() {
		EachGrid c = new EachGrid();
		c.setImage(getResources().getDrawable(R.drawable.no));
		CartoonList.add(c);

		c = new EachGrid();
		c.setImage(getResources().getDrawable(R.drawable.no));
		CartoonList.add(c);

		c = new EachGrid();
		c.setImage(getResources().getDrawable(R.drawable.no));
		CartoonList.add(c);

		c = new EachGrid();
		c.setImage(getResources().getDrawable(R.drawable.no));
		CartoonList.add(c);

		c = new EachGrid();
		c.setImage(getResources().getDrawable(R.drawable.no));
		CartoonList.add(c);

		c = new EachGrid();
		c.setImage(getResources().getDrawable(R.drawable.no));
		CartoonList.add(c);

		c = new EachGrid();
		c.setImage(getResources().getDrawable(R.drawable.no));
		CartoonList.add(c);

		c = new EachGrid();
		c.setImage(getResources().getDrawable(R.drawable.no));
		CartoonList.add(c);

		c = new EachGrid();
		c.setImage(getResources().getDrawable(R.drawable.no));
		CartoonList.add(c);
	}

	private void gameGridClicked(int position) {
		EachGrid c = CartoonList.get(position);

		if (c.getPlayerId() == 0) {
			if (player == 0) {
				c.setImage(getResources().getDrawable(R.drawable.x));
				c.setPlayerId(1);
				playedGrid.add(position);
				player1Marks++;
				player = 1;
				if (gameMode == 1) {
					analyzeGame();
					computerTurn();
					return;
				}
			} else {
				c.setImage(getResources().getDrawable(R.drawable.o));
				c.setPlayerId(2);
				playedGrid.add(position);
				player2Marks++;
				player = 0;				
			}

			adapter.notifyDataSetChanged();
			analyzeGame();			
		} else {
			return;
		}
	}

	private void analyzeGame() {
		int x = 0, y = 0, z = 0;
		EachGrid c;
		if(gameOver)
			return;
		if (player1Marks >= 3 || player2Marks >= 3) {
			for (int i = 0; i < 8; i++) {
				x = y = z = 0;
				for (int j = 0; j < 3; j++) {
					c = CartoonList.get(winning[i][j]);
					x = c.getPlayerId();
					if (x == 1) {
						y++;
					} else if (x == 2) {
						z++;
					}
				}
				if (y == 3) {
					Toast.makeText(getApplicationContext(), "Player X has won",
							Toast.LENGTH_LONG).show();
					gameOver = true;
					finish();
					return;
				} else if (z == 3) {
					Toast.makeText(getApplicationContext(), "Player O has won",
							Toast.LENGTH_LONG).show();
					gameOver = true;
					finish();
					return;
				}
			}
		}
		Log.e("total",String.valueOf(player1Marks + player2Marks));
		if ((player1Marks + player2Marks) == 9) {
			gameOver = true;
			Log.e("total",String.valueOf(player1Marks + player2Marks));
			Toast.makeText(getApplicationContext(), "Game draw",
					Toast.LENGTH_LONG).show();
			finish();
			return;
		}
	}

	private void computerTurn() {
		if(gameOver)
			return;
		if ((player1Marks + player2Marks) == 9) {
			Toast.makeText(getApplicationContext(), "Game draw",
					Toast.LENGTH_LONG).show();
			finish();
			return;
		}

		Random rand = new Random();
		int num;

		while (true) {
			num = rand.nextInt(8) + 1;
			if (playedGrid.indexOf(num) == -1) {
				gameGridClicked(num);
				break;
			}
		}
	}
}
